import { createStore } from 'vuex'
import vuexPre from 'vuex-persistedstate'

export default createStore({
  state: {
    shopList:[],//存放购物车数据
  },
  getters: {
    getPrice(state){
        let total=0
       state.shopList.forEach(item=>{
        if(item.status){
            total+=item.price*item.carNum
        }

       })
       return total
    }
  },
  mutations: {
    addCart(state,obj){
        console.log(obj);
        let items = state.shopList.find(item=>item.id == obj.id && item.ticName == obj.ticName)
        if(items){
            items.carNum++
        }else{
            state.shopList.push(obj)
        }
        console.log(state.shopList);
    },
    selectAll(state,all){
        state.shopList.forEach(item=>{
            item.status = all
        })

    },
    del(state,index){
        state.shopList.splice(index,1)

    },
    selectDel(state){
        // 留下不删除的
        state.shopList = state.shopList.filter(item=>item.status == false)

    },
    editOrder(state){
       state.shopList.forEach(item=>{
        // 把用户选中的商品的order变为true  只有order为true，才表示提交订单的商品
        if(item.status == true){
            item.order = true
        }
       })
    }
  },
  actions: {
  },
  modules: {
  },
  plugins:[vuexPre()]
})
